package com.zky.gulimall.ware.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zky.common.utils.PageUtils;
import com.zky.common.utils.Query;

import com.zky.gulimall.ware.dao.PurchaseDetailDao;
import com.zky.gulimall.ware.entity.PurchaseDetailEntity;
import com.zky.gulimall.ware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        Object key = params.get("key");
        if (Objects.nonNull(key)) {
            String key1 = (String) key;
            if (!StringUtils.isEmpty(key1)) {
                queryWrapper.and(e -> {
                    e.eq("purchase_id", key1).or().eq("sku_id", key1);
                });
            }
        }

        Object status = params.get("status");
        if (Objects.nonNull(status)) {
            String status1 = (String) status;
            if (!StringUtils.isEmpty(status1)) {
                queryWrapper.eq("status", status1);
            }
        }

        Object wareId = params.get("wareId");
        if (Objects.nonNull(wareId)) {
            String wareId1 = (String) wareId;
            if (!"0".equalsIgnoreCase(wareId1) && !StringUtils.isEmpty(wareId1)) {
                queryWrapper.eq("ware_id", wareId1);
            }
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        List<PurchaseDetailEntity> list = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
        return list;
    }

}